/**
 * FileName: JWTUtils
 * Author:   P
 * Date:     2020/9/22 22:24
 * Description:
 */
package com.hospital.nurse.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtils {

    private static final String SING = "#FSDF$#%EASD%&%^YGHDf";

    //生成token  header.payload.sing

    public static String getToke(Map<String,String> map){

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,3);//设置过期时间为三天
        JWTCreator.Builder builder = JWT.create();

        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });

        String token = builder.withExpiresAt(instance.getTime())   //指定过期时间
                .sign(Algorithm.HMAC256(SING));
        return token;
    }

    //验证token 的合法性

    public static DecodedJWT verify(String token){
       return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

}
